//
//  MBGoodsDetailCommentTableViewCell.m
//  MobellCloud_ios
//
//  Created by cherry pu on 2017/12/28.
//  Copyright © 2017年 zhangXin. All rights reserved.
//

#import "MBGoodsDetailCommentTableViewCell.h"
#import "MBHeaderLinesImageContantView.h"

@interface MBGoodsDetailCommentTableViewCell ()
@property (nonatomic,weak)MBHeaderLinesImageContantView* imageContentView;
@property (nonatomic,strong)MASConstraint* noImageConstraint;

@property (nonatomic,weak)UIImageView* headerImageView;
@property (nonatomic,weak)UILabel* nameLabel;
@property (nonatomic,weak)UILabel* timeLabel;
@property (nonatomic,weak)UILabel* detailLabel;
@property (nonatomic,weak)MBHeaderLinesImageContantView* imageContantView;
@property (nonatomic,strong) UIImage* placeHolder;
@property (nonatomic,weak)UIButton* zanButton;


@end

@implementation MBGoodsDetailCommentTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = COLOR_BG;
        UIView* contantView = [[UIView alloc]init];
        contantView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:contantView];
        
        [contantView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 0, [UIView lf_sizeFromIphone6:10], 0));
            make.width.mas_lessThanOrEqualTo(SCREEN_WIDTH);
        }];
        
        UIImageView* headerImageView = [[UIImageView alloc]init];
        self.headerImageView = headerImageView;
        headerImageView.image = [[UIImage imageNamed:@"icon_8"] imageWithCornerRadius:27/2];
        [contantView addSubview:headerImageView];
        
        [headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(contantView).with.offset([UIView lf_sizeFromIphone6:15]);
            make.left.equalTo(contantView).with.offset([UIView lf_sizeFromIphone6:12]);
            make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:27], [UIView lf_sizeFromIphone6:27]));
        }];
        
        UILabel* nameLabel = [[UILabel alloc]init];
        self.nameLabel = nameLabel;
        nameLabel.text = @"安迪2017";
        nameLabel.font = [UIFont lf_systemFontOfSize:13];
        nameLabel.textColor = [UIColor colorWithR:40 G:40 B:40];
        [contantView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(headerImageView);
            make.left.equalTo(headerImageView.mas_right).with.offset([UIView lf_sizeFromIphone6:13]);
        }];
        
        UILabel* timeLabel = [[UILabel alloc]init];
        self.timeLabel = timeLabel;
        timeLabel.text = @"2017-11-11 15:13:39";
        timeLabel.font = [UIFont lf_systemFontOfSize:12];
        timeLabel.textColor = [UIColor colorWithR:153 G:153 B:153];
        [contantView addSubview:timeLabel];
        
        [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(nameLabel);
            make.right.equalTo(contantView).with.offset([UIView lf_sizeFromIphone6:-13]);
        }];
        
        UILabel* detailLabel = [[UILabel alloc]init];
        self.detailLabel = detailLabel;
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:[UIView lf_sizeFromIphone6:3]];

        NSMutableAttributedString* commentAtt = [[NSMutableAttributedString alloc]initWithString:@"产品包装里里外外很到位，这次天猫国际买的几单，发货都好快。赞一个。还没有喝，口感不好评价，希望好喝" attributes:@{NSFontAttributeName:[UIFont lf_systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithR:40 G:40 B:40],NSParagraphStyleAttributeName:paragraphStyle}];
        detailLabel.attributedText = commentAtt;
        detailLabel.numberOfLines = 0;
        [contantView addSubview:detailLabel];
        
        [detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(contantView).with.offset([UIView lf_sizeFromIphone6:13]);
            make.top.equalTo(headerImageView.mas_bottom).with.offset([UIView lf_sizeFromIphone6:10]);
            make.width.mas_equalTo(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:26]);
        }];
        
        MBHeaderLinesImageContantView* imageContentView = [[MBHeaderLinesImageContantView alloc]initWithImageWidth:(SCREEN_WIDTH-[UIView lf_sizeFromIphone6:26])/3];
        self.imageContentView = imageContentView;
        [contantView addSubview:imageContentView];
        
        [imageContentView mas_makeConstraints:^(MASConstraintMaker *make) {
            self.noImageConstraint = make.top.equalTo(detailLabel.mas_bottom).with.offset([UIView lf_sizeFromIphone6:10]);
            make.left.equalTo(contantView).with.offset([UIView lf_sizeFromIphone6:10]);
            make.top.equalTo(detailLabel.mas_bottom).priorityLow();
        }];
        
        
        [contantView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(imageContentView.mas_bottom).with.offset([UIView lf_sizeFromIphone6:15]).priorityHigh();
        }];
        
        @weakify(self);
        [[RACObserve(imageContentView, imageIndex) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(id  _Nullable x) {
            @strongify(self);
            int index = [x intValue];
            if (index != 0) {
                if (self.delegate && [self.delegate respondsToSelector:@selector(goodsDetailCommentCellClickImageWithIndex:withModel:)]) {
                    [self.delegate goodsDetailCommentCellClickImageWithIndex:index-100 withModel:self.commentModel];
                }
            }
        }];

    }
    return self;
}


-(UIImage *)placeHolder
{
    if (_placeHolder == nil) {
        _placeHolder = [UIImage imageNamed:@"moren"];
    }
    return _placeHolder;
}


-(void)setCommentModel:(MBCommentModel *)commentModel
{
    _commentModel = commentModel;
    NSString* imageUrl = commentModel.head_pic;
    if (![imageUrl containsString:@"http"]) {
        imageUrl = [NSString stringWithFormat:@"%@%@",BASE_IMAGE_URL,commentModel.head_pic];
    }

    UIImage* headerImage = [[MBCacheService sharedInstance]fetchHeaderWithHeaderName:commentModel.head_pic];
    if (headerImage) {
        self.headerImageView.image = headerImage;
    }else {
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:self.placeHolder completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                image = [UIImage circleImage:image];
                [[MBCacheService sharedInstance]cacheHeaderWithHeaderName:commentModel.head_pic withHeader:image];
                [[SDImageCache sharedImageCache]clearMemory];
            }
        }];
    }
    self.nameLabel.text = commentModel.nickname;
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:[UIView lf_sizeFromIphone6:3]];

    NSMutableAttributedString* commentAtt = [[NSMutableAttributedString alloc]initWithString:commentModel.content attributes:@{NSFontAttributeName:[UIFont lf_systemFontOfSize:12],NSForegroundColorAttributeName:[UIColor colorWithR:40 G:40 B:40],NSParagraphStyleAttributeName:paragraphStyle}];
    self.detailLabel.attributedText = commentAtt;
    
    self.timeLabel.text = [NSString getChatTimeString:commentModel.add_time];
    
    self.imageContentView.imagePathArray = commentModel.img;
    
    if (commentModel.img.count == 0) {
        [self.noImageConstraint deactivate];
        self.imageContantView.hidden = YES;
    }else {
        [self.noImageConstraint activate];
        self.imageContantView.hidden = NO;
    }
    
    [self.zanButton setTitle:[NSString stringWithFormat:@"%ld",(long)commentModel.zan_num] forState:UIControlStateNormal];
    
    [self.zanButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.zanButton.intrinsicContentSize.width+[UIView lf_sizeFromIphone6:5]);
    }];
    if (commentModel.zan_status == 1) {
        self.zanButton.selected = YES;
    }else {
        self.zanButton.selected = NO;
    }

}



@end
